<?php
namespace app\admin\controller;
use think\Db;//导入数据库类 (DB类)
use think\Controller;
class Jurisdiction extends Admin
{


	//管理员设置
	public function index()
    {
    	$data=db('admin')->select();
    	$this->assign('data',$data);
       	return view('Jurisdiction/index');
        
    }

    public function add()
    {
    		
    		return view('Jurisdiction/indexadd');
    }

    public function doadd()
    {
    	if (empty(input('name')&&input('pwd'))) {
    		$this->error('输入信息有误');
    	}
    	$data = ['name' =>input('name'), 'pwd' => input('pwd')];
    	db('admin')->insert($data);
    	$this->success('添加成功');
    }


     public function del()
     {
     	$data=db('admin')->where('id',input('id'))->delete();
     	$this->success('删除成功');
    
     }

    public function edit()
     {
     	$data=db('admin')->where('id',input('id'))->find();
    	$this->assign('data',$data);
       	return view('Jurisdiction/edit');
     }

     public function doedit()
     {
     	if (empty(input('name')&&input('pwd'))) {
    		$this->error('输入信息有误');
    	}
    	$data = ['name' =>input('name'), 'pwd' => input('pwd')];
    	$rus=db('admin')->where('id',input('id'))->update($data);
    	if ($rus>0) {
    		$this->success('修改成功');
    	}else{
    		$this->error('错误');
    	}
    	
     }


      public function jobset()
     {
        //查询当前用户信息
        $data=db('admin')->where('id',input('id'))->find();
     
        //查询节点信息
        $list = db('role')->select();
           
          

        //获取当前用户的角色信息
        $rolelist = db('admin_role')->where('uid',input('id'))->select();
     

            $myrole = array(); //定义空数组
            //对用户的角色进行重组
            foreach($rolelist as $v){
                $myrole[] = $v['rid'];
            }
            

            //分配数据
            $this->assign("list",$list);
            //分配当前用户信息
            $this->assign('data',$data);
            //分配该用户的角色信息
            $this->assign('role',$myrole);


        return view('Jurisdiction/jobset');
     }

    //保存用户角色
        public function saverole(){
            
            //判读必须选择一个角色
            if(empty($_POST['role'])){
                $this->error("请选择一个角色！");
            }


            //清除用户所有的角色信息，避免重复添加
          db('admin_role')->where('uid',input('id'))->delete();
    
            foreach($_POST['role'] as $v){
                $data['uid'] = input('id');
                $data['rid'] = $v;
                //执行添加
                db('admin_role')->data($data)->insert();

            }

            $this->success("角色分配成功");
            
        } 
        
   
}



	
